# Read Action Day Data --------------------------------

act_day <- read.csv("data/raw/actionday/ActionDayForecasts.csv")
# Sort data by Reporting Area / State / Date
act_day <- act_day %>% dplyr::arrange(ReportingArea, State, IssueDate) %>% 
  dplyr::mutate(IssueDate = as.Date(act_day$IssueDate),
                ForecastDate = as.Date(act_day$ForecastDate))

# Join ReportingArea to census 2010 county by lat/lon
# Coordinate reference systems
NAD83 <- "+proj=longlat +datum=NAD83 +no_defs"
Albers<- "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs"

cnty2010 <- paste(wd, "/data/proc/tiger/tiger_line/county/2010/tl_2010_us_county10.rds", sep="") %>% 
  readRDS() %>% 
  dplyr::select(GEOID10) 

# Body: Convert actionday to sf -------------------------------------------------------------
actiondays <- act_day %>%
  st_as_sf(coords=c('Longitude','Latitude'), crs=NAD83) %>%
  st_transform(Albers)

(st_crs(cnty2010) == st_crs(Albers)) %>% stopifnot()

# Spatial join with action days
actiondays_cnty <- st_join(actiondays, cnty2010, join = st_within) %>% 
  st_drop_geometry() %>%
  type.convert(.,as.is=TRUE)

# Convert IssueDate & Forecast Date from character variable to numeric variable
actiondays_cnty$IssueDate <- as.Date(actiondays_cnty$IssueDate)
actiondays_cnty$ForecastDate <- as.Date(actiondays_cnty$ForecastDate)

actiondays_cnty_nodup <- actiondays_cnty %>% 
  dplyr::arrange(GEOID10, ForecastDate) %>% 
  group_by(GEOID10, ForecastDate) %>% 
  dplyr::mutate(id = row_number()) %>% 
  dplyr::filter(id == 1) %>% 
  ungroup() %>% 
  group_by(GEOID10) %>% 
  dplyr::mutate(dt_diff = ForecastDate - dplyr::lag(ForecastDate)) %>%
  dplyr::filter(dt_diff > 30) %>%
  dplyr::select(-c('dt_diff')) %>%
  ungroup() %>% 
  dplyr::mutate(year = lubridate::year(ForecastDate))


# Add Site Number and POC info to warning data
assign(paste("act_day_keep", poll, sep = "_"), actiondays_cnty_nodup %>% inner_join(get(paste("data_annual", poll, sep="_")) %>% 
        distinct(year, GEOID10, Site.Num, POC), by = c("year", "GEOID10")) %>%
  dplyr::arrange(GEOID10, Site.Num, POC, year) %>%
  drop_na(POC) %>% dplyr::mutate(month=lubridate::month(ForecastDate)) %>% 
  inner_join(get(paste("data_daily_comb",poll, sep="_")) %>% dplyr::mutate(month=lubridate::month(TrueDate)) %>%
               distinct(year, month, GEOID10, Site.Num, POC), by = c("year", "GEOID10", "Site.Num", "POC", "month")) %>%
  dplyr::arrange(GEOID10, Site.Num, POC, year))


assign(paste("act_day_exp", poll, sep = "_"), get(paste("act_day_keep", poll, sep = "_")) %>% group_by_all() %>% 
         tidyr::expand(eday = seq(-30, 30, by = 1)) %>%
         ungroup() %>% dplyr::arrange(GEOID10, Site.Num, POC, year) %>%
         dplyr::arrange(GEOID10, Site.Num, POC, ForecastDate, eday) %>% 
         dplyr::mutate(TrueDate = ForecastDate + eday, 
                       ActionDayFlag = ifelse(eday !=0, 0, 1),
                       d_m3p3 = ifelse(abs(eday) <= 3, 1, 0), 
                       d_m7p7 = ifelse(abs(eday) <= 7, 1, 0),
                       year = lubridate::year(TrueDate),
                       month=lubridate::month(TrueDate)) %>%
         inner_join(get(paste("data_daily_comb",poll, sep="_")) %>% dplyr::mutate(month=lubridate::month(TrueDate)) %>%
                      distinct(year, month, GEOID10, Site.Num, POC), by = c("year", "GEOID10", "Site.Num", "POC", "month")))

# save data file to interim folder
save(list=paste("act_day_exp",poll,sep="_"), file = paste("data/interim/", poll, "/warning_exp_evnt30_", poll,".RData",sep=""))
